<template>
	<view>
		<uni-section title="气象预警" type="line">
			<uni-card>
				<view style="font-weight: bolder;">{{warn.title}}</view>
				
			</uni-card>
					<uni-card :is-shadow="false">
						<view class="title2" style="text-indent: 2em;">{{warn.text}}</view>
						<view style="margin-top: 10px;display: flex;;justify-content: flex-end;">{{warn.pubTime}}</view>
					</uni-card>
				</uni-section>
	</view>
</template>

<script>
	const APIKEY = '7cc94edca9f74113adc43d1822d933c0' ;// 填入你申请的KEY
	export default{
		data() {
			return {
				user: uni.getStorageSync('weather2024'),
					currenttime:'',
					location: '',
					type:'',
					now: '',
					hourly: '',
					daily: '',
					City: '',
					County: '',
				tempMax:'',
				tempMin:'',
				warn:''
				
				}
				
	},
	onLoad() {
		this.getLocation();
		
		},
	methods: {
		//选择定位
			selectLocation() {
				var that = this;
				uni.chooseLocation({
					success(res) {
						that.location = res.longitude + ',' + res.latitude;
						that.getWeather();
						
						that.getCityByLoaction();
						that.getWeatherWarning();
					},
					fail() {
						uni.getLocation({
							type: 'gcj02',
							fail() {
								uni.showModal({
									title: '获取地图位置失败',
									content:
										'为了给您提供准确的天气预报服务,请在设置中授权【位置信息】',
									success(mRes) {
										if (mRes.confirm) {
											uni.openSetting({
												success: function (data) {
													if (
														data.authSetting['scope.userLocation'] === true
													) {
														that.selectLocation();
													} else {
														uni.showToast({
															title: '授权失败',
															icon: 'none',
															duration: 1000
														});
													}
												},
												fail(err) {
													console.log(err);
													uni.showToast({
														title: '唤起设置页失败，请手动打开',
														icon: 'none',
														duration: 1000
													});
												}
											});
										}
									}
								});
							}
						});
					}
				});
			},
			
			
			
			/**
			 * 获取定位
			 */
			getLocation() {
				var that = this;
				uni.getLocation({
					type: 'gcj02',
					success(res) {
						that.location = res.longitude + ',' + res.latitude;
						that.getWeather();
						that.getCityByLoaction();
						
						that.getWeatherWarning();
					},
					fail(err) {
						uni.showModal({
							title: '获取定位信息失败',
							content: '为了给您提供准确的天气预报服务,请在设置中授权【位置信息】',
							success(mRes) {
								if (mRes.confirm) {
									uni.openSetting({
										success: function (data) {
											if (data.authSetting['scope.userLocation'] === true) {
												uni.showToast({
													title: '授权成功',
													icon: 'success',
													duration: 1000
												});
												that.getLocation();
											} else {
												uni.showToast({
													title: '授权失败',
													icon: 'none',
													duration: 1000
												});
												that.location = '地理位置';
												that.getWeather();
												that.getCityByLoaction();
											}
										},
										fail(err) {
											console.log(err);
											uni.showToast({
												title: '唤起设置页失败，请手动打开',
												icon: 'none',
												duration: 1000
											});
											that.location = '地理位置';
											that.getWeather();
											that.getCityByLoaction();
										}
									});
								} else if (mRes.cancel) {
									that.location = '地理位置';
									that.getWeather();
									that.getCityByLoaction();
								}
							}
						});
					}
				});
			},
			/**
			 * 根据坐标获取城市信息
			 */
			getCityByLoaction() {
				var that = this;
				uni.request({
					url:
						'https://geoapi.qweather.com/v2/city/lookup?key=' +
						APIKEY +
						'&location=' +
						that.location,
					success(result) {
						var res = result.data;
						if (res.code == '200') {
							var data = res.location[0];
							that.City = data.adm2;
							that.County = data.name;
						} else {
							uni.showToast({
								title: '获取城市信息失败',
								icon: 'none'
							});
						}
					}
				});
			},
			/**
			 * 获取天气
			 */
			getWeather() {
				var that = this;
				uni.showLoading({
					title: '加载中'
				});
				uni.request({
					url:
						'https://devapi.qweather.com/v7/weather/now?key=' +
						APIKEY +
						'&location=' +
						that.location,
					success(result) {
						var res = result.data;
						that.now = res.now;
					}
				});
				uni.request({
					url:
						'https://devapi.qweather.com/v7/weather/24h?key=' +
						APIKEY +
						'&location=' +
						that.location,
					success(result) {
						var res = result.data;
						res.hourly.forEach(function (item) {
							item.time = that.formatTime(new Date(item.fxTime)).hourly;
						});
						that.hourly = res.hourly;
					}
				});
				uni.request({
					url:
						'https://devapi.qweather.com/v7/weather/7d?key=' +
						APIKEY +
						'&location=' +
						that.location,
					success(result) {
						var res = result.data;
						res.daily.forEach(function (item) {
							item.date = that.formatTime(new Date(item.fxDate)).daily;
							item.dateToString = that.formatTime(new Date(item.fxDate)).dailyToString;
						});
						that.tempMax=res.daily[0].tempMax;
						that.tempMin=res.daily[0].tempMin;
						
						that.daily = res.daily;
						uni.hideLoading();
					}
				});
			},
			getWeatherWarning(){
				var that = this;
				uni.request({
					url:
						'https://devapi.qweather.com/v7/warning/now?key=' +
						APIKEY +
						'&location=' +that.location,
						success(result){
							console.log(result);
							var res=result.data;
							that.warn=res.warning[0];
						
						
						}
				});
			},
			// 格式时间
			formatTime(date) {
				const year = date.getFullYear();
				const month = date.getMonth() + 1;
				const day = date.getDate();
				const hour = date.getHours();
				const minute = date.getMinutes();
				const second = date.getSeconds();
				const weekArray = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
				const isToday = date.setHours(0, 0, 0, 0) == new Date().setHours(0, 0, 0, 0);
				return {
					hourly: [hour, minute].map(this.formatNumber).join(':'),
					daily: [month, day].map(this.formatNumber).join('-'),
					dailyToString: isToday ? '今天' : weekArray[date.getDay()]
				};
			},
			// 补零
			formatNumber(n) {
				n = n.toString();
				return n[1] ? n : '0' + n;
			}
		
		
		
		},
		}
</script>

<style>
	.title2{
		 
				font-family:"Times New Roman", Times, serif;
				font-weight: bolder;
				color: black;
				font-size: 20px;
				display: flex;
				justify-content: center;
				align-items: center;
		}
</style>